﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using TomNet.IMSC.Core.Contracts.Organization;
using TomNet.IMSC.Model.DbModels.Organization;
using TomNet.Web.Mvc;
using TomNet.Web.Mvc.UI;

namespace TomNet.IMSC.Web.Areas.Organization.Controllers
{
    [Description("内勤模版")]
    public class SideController : BaseController
    {
        public ISideModuleContract SideModuleContract { get; set; }
        #region 列表
        [Description("内勤模版-列表")]
        public ActionResult Index()
        {
            return View();
        }
        [HttpPost]
        public ActionResult IndexAsync(int pageIndex, int pageSize, string search = "")
        {
            var query = SideModuleContract.Entities;

            var total = query.Count();
            //默认给一个排序的字段
            query = query.OrderBy(m => m.Id);

            //分页(假如total == 0，则取消分页查询，提高性能)
            query = total > 0 ? query.Skip((pageIndex - 1) * pageSize).Take(pageSize)
                : Enumerable.Empty<SideModule>().AsQueryable();

            //此处可以采用匿名对象 GridData<object>
            GridData<SideModule> gridData = new GridData<SideModule>() { Total = total, Rows = query.ToList() };

            //此处采用重写后的jsonResult
            return JsonEx(gridData, JsonRequestBehavior.AllowGet, "HH:mm:ss");
        }
        #endregion
        #region 添加
        [Description("内勤模版-添加")]
        public ActionResult Insert()
        {
            return View();
        }
        [HttpPost]
        public ActionResult InsertAsync(SideModule entity)
        {
            //所有AJAX的结果，返回统一数据格式
            var result = new AjaxResult();
            try
            {
                var count = SideModuleContract.Insert(entity);
                if (count > 0)
                {
                    result.Type = AjaxResultType.Success;
                    result.Content = "录入成功";
                }
                else
                {
                    result.Type = AjaxResultType.Error;
                    result.Content = "录入失败";
                }
            }
            catch
            {
                result.Type = AjaxResultType.Error;
                result.Content = "异常操作";
            }
            return JsonEx(result);
        }
        #endregion
        #region 修改
        [Description("内勤模版-修改")]
        public ActionResult Edit(int id)
        {
            var entity = SideModuleContract.GetByKey(id);
            //后台容错，有异常数据直接抛出。框架会自动跳转到错误页面。
            if (entity == null) throw new Exception("未找到该实体");
            var Start = entity.StartTime.ToString("HH:mm:ss");
            var End = entity.EndTime.ToString("HH:mm:ss");
            ViewBag.entity = entity;
            return View();
        }
        [HttpPost]
        public ActionResult EditAsync(SideModule entity)
        {
            var result = new AjaxResult();
            try
            {
                var count = SideModuleContract.Update(entity);
                if (count > 0)
                {
                    result.Type = AjaxResultType.Success;
                    result.Content = "修改成功";
                }
                else
                {
                    result.Type = AjaxResultType.Error;
                    result.Content = "修改失败";
                }
            }
            catch
            {
                result.Type = AjaxResultType.Error;
                result.Content = "异常操作";
            }
            return JsonEx(result);
        }
        #endregion
        #region 删除
        [Description("内勤模版-删除")]
        [HttpPost]
        public ActionResult DeleteAsync(string ids)
        {
            var result = new AjaxResult();
            try
            {
                string[] idsStr = ids.Split(new char[] { ',' });
                int[] idsInt = Array.ConvertAll(idsStr, id => Convert.ToInt32(id));
                for (int i = 0; i < idsInt.Length; i++)
                {
                    SideModuleContract.DeleteDirect(idsInt[i]);
                }

                result.Type = AjaxResultType.Success;
                result.Content = "删除成功";
            }
            catch
            {
                result.Type = AjaxResultType.Error;
                result.Content = "异常操作";
            }
            return JsonEx(result);
        } 
        #endregion
    }
}